<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>Aria: auxSerialExample.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Aria
   &#160;<span id="projectnumber">2.9.1</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('auxSerialExample_8cpp-example.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">auxSerialExample.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>Demonstrates the use of a robot packet handler to recieve data from a device attached to the robot microcontroller's auxilliary serial port.This example shows how to use the GETAUX command and how to recieve SERAUX serial port data. To use this example, you must have the auxilliary serial port AUX1 on the microcontroller (inside the robot) connected to a device that is sending text data.</p>
<p>You can connect AUX1 to a computer through an RS-232 cable with a NULL modem adapter, and run a program such as minicom (Linux) or HyperTerminal (Windows). Then just type to send data to the AUX1 port. Configure minicom or HyperTerminal to have the following serial port settings: 9600 baud 8 data bits, 1 stop bit, no parity No flow control (not hardware, not software)!</p>
<p>This program creates a packet handler function (handleAuxSerialPacket) and adds it to the <a class="el" href="classArRobot.html" title="Central class for communicating with and operating the robot. ">ArRobot</a> object. All packets recieved from the robot are passed to all packet handlers. handleAuxSerialPacket() checks whether the packet is an SERAUX packet, and if so, prints out the data contained within the packet. If a newline or carriage return character is recieved, then it sends a command to send data back out through the AUX serial port. The packet handler then sends a GETAUX command to the robot to request more data.</p>
<div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;Aria.h&quot;</span></div><div class="line"></div><div class="line"><span class="comment">// Amount of data (bytes) to request in each SERAUX packet</span></div><div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> SerAuxDataChunkSize = 24;</div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> handleAuxSerialPacket(<a name="_a0"></a><a class="code" href="classArRobotPacket.html">ArRobotPacket</a> *packet)</div><div class="line">{</div><div class="line">  <span class="comment">// If this is not an aux. serial data packet, then return false to allow other</span></div><div class="line">  <span class="comment">// packet handlers to recieve the packet. The packet type ID numbers are found</span></div><div class="line">  <span class="comment">// in the description of the GETAUX commands in the robot manual.</span></div><div class="line">  <span class="keywordflow">if</span> (packet-&gt;<a name="a1"></a><a class="code" href="classArRobotPacket.html#a0e41b0bbe73260cff302094500a51b7d">getID</a>() != 0xb0) <span class="comment">// 0xB0 is SERAUXpac. SERAUX2pac is 0xB8, SERAUX3pac is 0xC8.</span></div><div class="line">    <span class="keywordflow">return</span> <span class="keyword">false</span>;</div><div class="line"></div><div class="line">  <span class="comment">// Displays the contents of the packet</span></div><div class="line">  packet-&gt;<a name="a2"></a>log();</div><div class="line"></div><div class="line">  <span class="comment">// You can also extract each byte or sets of bytes </span></div><div class="line">  <span class="comment">// from the packet&#39;s &quot;data buffer&quot; in sequence.</span></div><div class="line">  <span class="comment">// As data is removed, getReadLength() is reduced accordingly.</span></div><div class="line">  <span class="comment">// getLength() returns the original packet length including   </span></div><div class="line">  <span class="comment">// ID byte and length byte.</span></div><div class="line">  <span class="keywordtype">int</span> n = 0;</div><div class="line">  <span class="keywordflow">while</span> (packet-&gt;<a name="a3"></a><a class="code" href="classArBasePacket.html#ae46601e750b76486525b09f7898dda94">getReadLength</a>() &lt; packet-&gt;<a name="a4"></a><a class="code" href="classArBasePacket.html#a1ffc091d20ab361fe67a664b322976a3">getLength</a>() - 2)</div><div class="line">  {</div><div class="line">    <span class="comment">// Values of different sizes can be removed from the packet:</span></div><div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c = packet-&gt;<a name="a5"></a><a class="code" href="classArBasePacket.html#a32d7bab938c3fcc1929890282eb5bec6">bufToUByte</a>();</div><div class="line">    <span class="comment">// unsigned int i = packet-&gt;bufToUByte2();</span></div><div class="line">    <span class="comment">// int i = packet-&gt;bufToByte2();</span></div><div class="line">    <span class="comment">// unsigned int i = packet-&gt;bufToUByte4();</span></div><div class="line">    <span class="comment">// int i = packet-&gt;bufToByte4();</span></div><div class="line">    <span class="comment">// unsigned long i = packet-&gt;bufToUByte8();</span></div><div class="line">    <span class="comment">// long i = packet-&gt;bufToByte8();</span></div><div class="line">    <span class="comment">// To get a NULL-terminated string:</span></div><div class="line">    <span class="comment">//std::string s = packet-&gt;bufToString();</span></div><div class="line">    ++n;</div><div class="line">  }</div><div class="line"></div><div class="line">  <a name="a6"></a><a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a7"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;auxSerialExample: handled SERAUX packet, extracted  %d fields of data&quot;</span>, n);</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// Return true to indicate to ArRobot that we have handled this packet.</span></div><div class="line">  <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line">}</div><div class="line">  </div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv) </div><div class="line">{</div><div class="line">  <a name="a8"></a><a class="code" href="classAria.html#ad99c16b5d947229d9f8e1c5b2d4cdd73">Aria::init</a>();</div><div class="line">  <a name="_a9"></a><a class="code" href="classArArgumentParser.html">ArArgumentParser</a> parser(&amp;argc, argv);</div><div class="line">  parser.<a name="a10"></a><a class="code" href="classArArgumentParser.html#a501694b62a7f8b0c3cadd84aeafe1b35">loadDefaultArguments</a>();</div><div class="line"></div><div class="line">  <a name="_a11"></a><a class="code" href="classArRobot.html">ArRobot</a> robot;</div><div class="line"></div><div class="line">  <span class="comment">// Connect to the robot, get some initial data from it such as type and name,</span></div><div class="line">  <span class="comment">// and then load parameter files for this robot.</span></div><div class="line">  <a name="_a12"></a><a class="code" href="classArRobotConnector.html">ArRobotConnector</a> robotConnector(&amp;parser, &amp;robot);</div><div class="line">  <span class="keywordflow">if</span>(!robotConnector.<a name="a13"></a><a class="code" href="classArRobotConnector.html#ab192ac393d9cc1e5b61b1a82b08d085d">connectRobot</a>())</div><div class="line">  {</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a14"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;auxSerialExample: Could not connect to the robot.&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span>(parser.<a name="a15"></a><a class="code" href="classArArgumentParser.html#ac3889cbdfa862031c51bf80e06954914">checkHelpAndWarnUnparsed</a>())</div><div class="line">    {</div><div class="line">        <span class="comment">// -help not given</span></div><div class="line">        <a name="a16"></a><a class="code" href="classAria.html#a10d71f3d4d0cf7f38c58a1f749f64a42">Aria::logOptions</a>();</div><div class="line">        <a name="a17"></a><a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(1);</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (!<a name="a18"></a><a class="code" href="classAria.html#a1b090c01d88bb420b8cf8e0384d25ee1">Aria::parseArgs</a>() || !parser.<a class="code" href="classArArgumentParser.html#ac3889cbdfa862031c51bf80e06954914">checkHelpAndWarnUnparsed</a>())</div><div class="line">  {</div><div class="line">    <a class="code" href="classAria.html#a10d71f3d4d0cf7f38c58a1f749f64a42">Aria::logOptions</a>();</div><div class="line">    <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// functor for the packet handler</span></div><div class="line">  <a name="_a19"></a><a class="code" href="classArGlobalRetFunctor1.html">ArGlobalRetFunctor1&lt;bool, ArRobotPacket *&gt;</a> handleAuxCB(&amp;handleAuxSerialPacket);</div><div class="line"></div><div class="line">  <span class="comment">// add our packet handler as the first one in the list</span></div><div class="line">  robot.<a name="a20"></a><a class="code" href="classArRobot.html#a9710774cf8be7f146ca58f83dd076e3f">addPacketHandler</a>(&amp;handleAuxCB, <a name="a21"></a><a class="code" href="classArListPos.html#adb42d52c70a646c5698c5513cd25bbd0a7573a55fbcdb54b1768e9ce574407d15">ArListPos::FIRST</a>);</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// Set BAUD rate of serial port</span></div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;auxSerialExample: Connected to the robot. Sending command to change AUX1 baud rate to 9600...&quot;</span>);</div><div class="line">  robot.<a name="a22"></a><a class="code" href="classArRobot.html#aceef80a6274fb34aafcc422e4b1778c1">comInt</a>(<a name="a23"></a><a class="code" href="classArCommands.html#a9a31f904a95bbc31d8b33c6637100cc4acbcdf7c6c50666489b721290d9440c34">ArCommands::AUX1BAUD</a>, 0); <span class="comment">// 0=9600. See robot manual for other rates</span></div><div class="line"></div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;Flushing robot&#39;s buffer...&quot;</span>);</div><div class="line">  robot.<a class="code" href="classArRobot.html#aceef80a6274fb34aafcc422e4b1778c1">comInt</a>(<a name="a24"></a><a class="code" href="classArCommands.html#a9a31f904a95bbc31d8b33c6637100cc4a10e72ab2eec33fe56d8db1042c462e93">ArCommands::GETAUX</a>, 0);</div><div class="line"></div><div class="line">  <span class="comment">// Send a GETAUX request every loop:</span></div><div class="line">  <span class="comment">// (Note, If you wanted to recieve information from the second aux. serial port, use</span></div><div class="line">  <span class="comment">// the GETAUX2 command instead; but the packet returned will also have a</span></div><div class="line">  <span class="comment">// different type ID.)</span></div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;auxSerialExample: Sending GETAUX request every robot task loop for %d bytes from aux1 serial port...&quot;</span>, SerAuxDataChunkSize);</div><div class="line"></div><div class="line">  <a class="code" href="classArGlobalRetFunctor1.html">ArGlobalRetFunctor1&lt;int, const char*&gt;</a> putsCB(&amp;::puts, <span class="stringliteral">&quot;requesting aux data...&quot;</span>);</div><div class="line">  robot.<a name="a25"></a><a class="code" href="classArRobot.html#a50070f4e19b91aaac20772d15d9ad805">addUserTask</a>(<span class="stringliteral">&quot;Msg&quot;</span>, <a name="a26"></a><a class="code" href="classArListPos.html#adb42d52c70a646c5698c5513cd25bbd0a3c55111fa6c7f63c41fc0b387f5b57d9">ArListPos::LAST</a>-2, &amp;putsCB);</div><div class="line"></div><div class="line">  <span class="comment">//ArRetFunctor2C&lt;bool, ArRobot, unsigned char, short int&gt; flushAuxCB(&amp;robot, &amp;ArRobot::comInt, ArCommands::GETAUX, 0);</span></div><div class="line">  <span class="comment">//robot.addUserTask(&quot;FlushAux&quot;, ArListPos::LAST-1, &amp;flushAuxCB);</span></div><div class="line"></div><div class="line">  <a name="_a27"></a><a class="code" href="classArRetFunctor2C.html">ArRetFunctor2C&lt;bool, ArRobot, unsigned char, short int&gt;</a> requestAuxCB(&amp;robot, &amp;<a class="code" href="classArRobot.html#aceef80a6274fb34aafcc422e4b1778c1">ArRobot::comInt</a>, <a class="code" href="classArCommands.html#a9a31f904a95bbc31d8b33c6637100cc4a10e72ab2eec33fe56d8db1042c462e93">ArCommands::GETAUX</a>, SerAuxDataChunkSize);</div><div class="line">  robot.<a class="code" href="classArRobot.html#a50070f4e19b91aaac20772d15d9ad805">addUserTask</a>(<span class="stringliteral">&quot;GetAux&quot;</span>, <a class="code" href="classArListPos.html#adb42d52c70a646c5698c5513cd25bbd0a3c55111fa6c7f63c41fc0b387f5b57d9">ArListPos::LAST</a>, &amp;requestAuxCB);</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// How to send data to the serial port. See robot manual</span></div><div class="line">  <span class="comment">// (but note that TTY2 is for the AUX1 port, TTY3 for AUX2, etc.)</span></div><div class="line">  <span class="comment">//robot.comStr(ArCommands::TTY2, &quot;Hello, World!\n\r&quot;);</span></div><div class="line">  <span class="comment">//ArTypes::Byte2 n = 23;</span></div><div class="line">  <span class="comment">//robot.comDataN(ArCommands::TTY2, &amp;n, sizeof(n));</span></div><div class="line"></div><div class="line">  <span class="comment">// run the robot until disconnect, then shutdown and exit.</span></div><div class="line">  robot.<a name="a28"></a><a class="code" href="classArRobot.html#a3c1c3056633caf455c8265259f57f6d7">run</a>(<span class="keyword">true</span>);</div><div class="line">  <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(0);</div><div class="line">  <span class="keywordflow">return</span> 0;  </div><div class="line">}</div><div class="line"></div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.11 </li>
  </ul>
</div>
</body>
</html>
